"use strict";
const common_vendor = require("../../../common/vendor.js");
const api_police_answer = require("../../../api/police/answer.js");
if (!Array) {
  const _easycom_up_toast2 = common_vendor.resolveComponent("up-toast");
  _easycom_up_toast2();
}
const _easycom_up_toast = () => "../../../node-modules/uview-plus/components/u-toast/u-toast.js";
if (!Math) {
  _easycom_up_toast();
}
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
  __name: "onlineAnswer",
  setup(__props) {
    const uToastRef = common_vendor.ref(null);
    const topicInfo = common_vendor.ref({});
    const selectedOptions = common_vendor.ref([]);
    const successToastRef = common_vendor.ref();
    const errorToastRef = common_vendor.ref();
    const getTopicInfo = () => {
      common_vendor.index.showLoading({
        title: "加载中",
        mask: true
      });
      api_police_answer.extractQuestion().then((res) => {
        common_vendor.index.hideLoading();
        const { data } = res;
        if (data.optionContent) {
          data.optionContent = JSON.parse(data.optionContent);
        } else {
          data.optionContent = [];
        }
        topicInfo.value = data;
      }).catch((err) => {
        common_vendor.index.hideLoading();
        uToastRef.value.show({
          type: "error",
          message: err
        });
      });
    };
    const changeSelectOptions = (op) => {
      if (selectedOptions.value.length) {
        if (selectedOptions.value.indexOf(op) > -1) {
          selectedOptions.value.splice(selectedOptions.value.indexOf(op), 1);
        } else {
          selectedOptions.value.push(op);
        }
      } else {
        selectedOptions.value.push(op);
      }
      selectedOptions.value.sort(
        (a, b) => a.localeCompare(b, "en", { sensitivity: "base" })
      );
    };
    const onsubmit = () => {
      let selectAnswer = selectedOptions.value.join("");
      if (selectAnswer) {
        if (selectAnswer === topicInfo.value.correctAnswer) {
          successToastRef.value.show({
            type: "success",
            message: "回答正确",
            position: "bottom",
            icon: "checkmark-circle-fill",
            complete() {
              common_vendor.index.redirectTo({
                url: "/pages/police/home/index"
              });
            }
          });
        } else {
          errorToastRef.value.show({
            type: "error",
            message: `回答错误，正确答案应选：${topicInfo.value.correctAnswer}，请重新答题并提交。`,
            position: "center",
            icon: "close-circle-fill",
            complete() {
              selectedOptions.value = [];
            }
          });
        }
      } else {
        uToastRef.value.show({
          type: "warning",
          message: "请选择选项"
        });
      }
    };
    common_vendor.onLoad(() => {
      getTopicInfo();
    });
    return (_ctx, _cache) => {
      return {
        a: common_vendor.t(common_vendor.unref(topicInfo).type == 0 ? "单选" : common_vendor.unref(topicInfo).type == 1 ? "多选" : ""),
        b: common_vendor.t(common_vendor.unref(topicInfo).topicContent),
        c: common_vendor.f(common_vendor.unref(topicInfo).optionContent, (op, opIndex, i0) => {
          return {
            a: common_vendor.t(op.option),
            b: common_vendor.t(op.optionCount),
            c: common_vendor.n(common_vendor.unref(selectedOptions).indexOf(op.option) > -1 ? "active flex flex-row items-center text-_a_26rpx_a_ px-3 py-2 box-border mb-2_a_5 options-item" : "unactive flex flex-row items-center text-_a_26rpx_a_ px-3 py-2 box-border mb-2_a_5 options-item"),
            d: opIndex,
            e: common_vendor.o(($event) => common_vendor.unref(topicInfo).type == 0 ? selectedOptions.value = [op.option] : changeSelectOptions(op.option), opIndex)
          };
        }),
        d: common_vendor.o(onsubmit),
        e: common_vendor.sr(successToastRef, "6f9e81ee-0", {
          "k": "successToastRef"
        }),
        f: common_vendor.sr(errorToastRef, "6f9e81ee-1", {
          "k": "errorToastRef"
        }),
        g: common_vendor.sr(uToastRef, "6f9e81ee-2", {
          "k": "uToastRef"
        })
      };
    };
  }
});
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-6f9e81ee"]]);
wx.createPage(MiniProgramPage);
